package com.zbkj.crmeb.front.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.crmeb.core.common.CommonResult;
import com.constants.Constants;
import com.utils.CrmebUtil;
import com.crmeb.core.utils.RedisUtil;
import com.zbkj.crmeb.authorization.model.TokenModel;
import com.zbkj.crmeb.front.response.LoginResponse;
import com.zbkj.crmeb.front.service.UserCenterService;
import com.zbkj.crmeb.merchant.api.controller.ApiBaseController;
import com.zbkj.crmeb.user.request.AuthorizePhoneRequest;
import com.zbkj.crmeb.user.request.RegisterThirdUserRequest;
import com.zbkj.crmeb.wechat.model.TemplateMessage;
import com.zbkj.crmeb.wechat.service.TemplateMessageService;
import com.zbkj.crmeb.wechat.service.WeChatService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


/**
 * 微信缓存表 前端控制器
 */
@Slf4j
@RestController("WeChatFrontController")
@RequestMapping("api/front/wechat")
@Api(tags = "微信 -- 开放平台")
public class WeChatController extends ApiBaseController {
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private UserCenterService userCenterService;
    @Autowired
    private TemplateMessageService templateMessageService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取授权页面跳转地址
     * @author Mr.Zhang
     * @since 2020-05-25
     */
    @ApiOperation(value = "获取授权页面跳转地址")
    @RequestMapping(value = "/authorize/get", method = RequestMethod.GET)
    public CommonResult<Object> get(){
        return CommonResult.success(weChatService.getAuthorizeUrl());
    }

    /**
     * 通过微信code登录
     * @author Mr.Zhang
     * @since 2020-05-25
     */
    @ApiOperation(value = "微信登录公共号授权登录")
    @RequestMapping(value = "/authorize/login", method = RequestMethod.GET)
    public CommonResult<LoginResponse> login(@RequestParam(value = "spread_spid", defaultValue = "0", required = false) Integer spreadUid,
                                             @RequestParam(value = "code") String code, HttpServletRequest servletRequest){
        return CommonResult.success(userCenterService.weChatAuthorizeLogin(code, spreadUid, getMerId(servletRequest)));
    }

    /**
     * 通过小程序code登录
     * @author Mr.Zhang
     * @since 2020-05-25
     */

    // 微信小程序登录，获取token
    @ApiOperation(value = "微信登录小程序授权登录")
    @RequestMapping(value = "/authorize/program/login", method = RequestMethod.POST)
    public CommonResult<LoginResponse> programLogin(@RequestParam String code, @RequestBody @Validated RegisterThirdUserRequest registerThirdUserRequest, HttpServletRequest request){
    	// 防止高频调用接口
		log.info("--------------- 用户登录, 参数: {} ---------------", JSON.toJSONString(registerThirdUserRequest));
    	if(redisUtil.exists("authorize:" + code)) {
    		log.info("--------------- 2s高频调用ip: {}, 直接返回 ---------------", CrmebUtil.getClientIp(request));
    		return null;
    	}
        redisUtil.set("authorize:" + code, code, 2l, TimeUnit.SECONDS);
    	
    	registerThirdUserRequest.setMerId(getMerId(request));
        return CommonResult.success(userCenterService.weChatAuthorizeProgramLogin(code, registerThirdUserRequest));
    }

    /**
     * 微信登录小程序 - 授权获取用户手机号
     * @author Howe Hsiang
     * @since 2021-01-27
     */
    @ApiOperation(value = "微信登录小程序 - 授权获取用户手机号")
    @RequestMapping(value = "/authorize/program/getPhone", method = RequestMethod.POST)
    public CommonResult<String> getAuthorizePhone(@RequestBody @Validated AuthorizePhoneRequest request){
        if (userCenterService.getAuthorizePhone(request)){
            return CommonResult.success("获取成功!");
        }
        return CommonResult.failed("获取失败!");
    }


    /**
     * 获取微信公众号js配置
     * @author Mr.Zhang
     * @since 2020-05-25
     */
    @ApiOperation(value = "获取微信公众号js配置")
    @RequestMapping(value = "/config", method = RequestMethod.GET)
    @ApiImplicitParam(name = "url", value = "页面地址url")
    public CommonResult<Object> configJs(@RequestParam(value = "url") String url){
        return CommonResult.success(weChatService.getJsSdkConfig(url));
    }

    /**
     * 小程序获取授权logo
     * @author Mr.Zhang
     * @since 2020-05-25
     */
    @ApiOperation(value = "小程序获取授权logo")
    @RequestMapping(value = "/getLogo", method = RequestMethod.GET)
    public CommonResult<Map<String, String>> getLogo(){
        Map<String, String> map = new HashMap<>();
        map.put("logoUrl", userCenterService.getLogo());
        return CommonResult.success(map);
    }

    /**
     * 查询微信模板信息
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-06-03
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public CommonResult<TemplateMessage> info(@PathVariable Integer id){
        TemplateMessage templateMessage = templateMessageService.infoException(id);
        return CommonResult.success(templateMessage);
    }
}



